package com.qmhl.toolposition.adapter.msg

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.qmhl.toolposition.api.content.FriendMsg
import com.qmhl.toolposition.databinding.ItemFriendArgeeMsgBinding
import com.qmhl.toolposition.databinding.ItemFriendIsArgeeFaqifangMsgBinding
import com.qmhl.toolposition.databinding.ItemFriendIsArgeeMsgBinding
import com.qmhl.toolposition.databinding.ItemFriendRefuseFaqifangMsgBinding
import com.qmhl.toolposition.databinding.ItemFriendRefuseMsgBinding
import com.qmhl.toolposition.databinding.ItemFriendSendInviteMsgBinding

class FriendMsgAdapter(data: List<FriendMsg>) :
    BaseMultiItemAdapter<FriendMsg>(data) {

    var innerListener: OnItemClickListener? = null

    fun setInnerViewClickListener(clickListener: OnItemClickListener) {
        this.innerListener = clickListener
    }


    // 发送好友申请 viewholder（发起方）
    class SendInviteVH(val viewBinding: ItemFriendSendInviteMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    // 是否同意
    class IsAgreeInviteVH(val viewBinding: ItemFriendIsArgeeMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    // 已同意
    class AgreeVH(val viewBinding: ItemFriendArgeeMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    // 已拒绝
    class RefuseVH(val viewBinding: ItemFriendRefuseMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    // 已同意发起方
    class AgreeFaVH(val viewBinding: ItemFriendIsArgeeFaqifangMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    // 已拒绝发起方
    class RefuseFaVH(val viewBinding: ItemFriendRefuseFaqifangMsgBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    // 在 init 初始化的时候，添加多类型
    init {
        addItemType(
            ITEM_SEND_INVITE_TYPE,
            object : OnMultiItemAdapterListener<FriendMsg, SendInviteVH> { // 类型  发送好友邀请
                override fun onCreate(
                    context: Context,
                    parent: ViewGroup,
                    viewType: Int
                ): SendInviteVH {
                    // 创建 viewholder
                    val viewBinding =
                        ItemFriendSendInviteMsgBinding.inflate(
                            LayoutInflater.from(context),
                            parent,
                            false
                        )
                    return SendInviteVH(viewBinding)
                }

                override fun onBind(holder: SendInviteVH, position: Int, item: FriendMsg?) {
                    // 绑定 item 数据
                    holder.viewBinding.tvTime.text = item!!.dateStr
                    val fromUser = item!!.fromUser
                    val msgContent = "已向${fromUser}提交好友申请，同意后，您与他将共享位置信息。";
                    holder.viewBinding.tvMsg.text = msgContent
                }
            })


            .addItemType(
                ITEM_IS_AGREE_TYPE,
                object : OnMultiItemAdapterListener<FriendMsg, IsAgreeInviteVH> { // 类型  是否同意
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): IsAgreeInviteVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemFriendIsArgeeMsgBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )


                        return IsAgreeInviteVH(viewBinding)
                    }

                    override fun onBind(holder: IsAgreeInviteVH, position: Int, item: FriendMsg?) {
                        // 绑定 item 数据
                        holder.viewBinding.tvTime.text = item!!.dateStr
                        val fromUser = item!!.fromUser
                        val msgContent = "${fromUser}想添加您为好友，同意后，您与他将共享位置信息。";
                        holder.viewBinding.tvMsg.text = msgContent


                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })


            .addItemType(
                ITEM_ALREADY_AGREE_TYPE,
                object : OnMultiItemAdapterListener<FriendMsg, AgreeVH> { // 类型  已同意
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): AgreeVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemFriendArgeeMsgBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return AgreeVH(viewBinding)
                    }

                    override fun onBind(holder: AgreeVH, position: Int, item: FriendMsg?) {
                        holder.viewBinding.tvTime.text = item!!.dateStr
                        val fromUser = item!!.fromUser
                        val msgContent = "您已同意了${fromUser}的好友请求，您与他将共享位置信息。";
                        holder.viewBinding.tvMsg.text = msgContent

                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })

            .addItemType(
                ITEM_ALREADY_REFUSE_TYPE,
                object : OnMultiItemAdapterListener<FriendMsg, RefuseVH> { //  已拒绝
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): RefuseVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemFriendRefuseMsgBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return RefuseVH(viewBinding)
                    }

                    override fun onBind(holder: RefuseVH, position: Int, item: FriendMsg?) {
                        holder.viewBinding.tvTime.text = item!!.dateStr
                        val fromUser = item!!.fromUser
                        val msgContent = "您已拒绝了${fromUser}的好友请求。";
                        holder.viewBinding.tvMsg.text = msgContent
                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })

            .addItemType(
                ITEM_ALREADY_AGREE_FAQIFANG_TYPE,
                object : OnMultiItemAdapterListener<FriendMsg, AgreeFaVH> { // 类型  已同意发起方
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): AgreeFaVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemFriendIsArgeeFaqifangMsgBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return AgreeFaVH(viewBinding)
                    }

                    override fun onBind(holder: AgreeFaVH, position: Int, item: FriendMsg?) {
                        holder.viewBinding.tvTime.text = item!!.dateStr
                        val fromUser = item!!.fromUser
                        val msgContent = "${fromUser}同意了您的好友请求，您与他将共享位置信息。";
                        holder.viewBinding.tvMsg.text = msgContent

                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })

            .addItemType(
                ITEM_ALREADY_REFUSE_FAQIFANG_TYPE,
                object : OnMultiItemAdapterListener<FriendMsg, RefuseFaVH> { //  已拒绝发起方
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): RefuseFaVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemFriendRefuseFaqifangMsgBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return RefuseFaVH(viewBinding)
                    }

                    override fun onBind(holder: RefuseFaVH, position: Int, item: FriendMsg?) {
                        holder.viewBinding.tvTime.text = item!!.dateStr
                        val fromUser = item!!.fromUser
                        val msgContent = "${fromUser}拒绝了您的好友请求。";
                        holder.viewBinding.tvMsg.text = msgContent
                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })

            .onItemViewType { position, list -> // 根据数据，返回对应的 ItemViewType
                val msg = list[position]
                if (msg.msgType == 1) {
                    ITEM_SEND_INVITE_TYPE
                } else if (msg.msgType == 2 && msg.status == 1) {
                    ITEM_IS_AGREE_TYPE
                } else if (msg.msgType == 2 && msg.status == 2) {
                    ITEM_ALREADY_AGREE_TYPE
                } else if (msg.msgType == 2 && msg.status == 3) {
                    ITEM_ALREADY_REFUSE_TYPE
                } else if (msg.msgType == 3) {
                    ITEM_ALREADY_AGREE_FAQIFANG_TYPE
                } else if (msg.msgType == 4) {
                    ITEM_ALREADY_REFUSE_FAQIFANG_TYPE
                } else {
                    ITEM_SEND_INVITE_TYPE
                }
            }
    }

    companion object {
        private const val ITEM_SEND_INVITE_TYPE = 1 //发送好友邀请
        private const val ITEM_IS_AGREE_TYPE = 2 //对方是否同意
        private const val ITEM_ALREADY_AGREE_TYPE = 3//已同意
        private const val ITEM_ALREADY_REFUSE_TYPE = 4//已拒绝

        private const val ITEM_ALREADY_AGREE_FAQIFANG_TYPE = 5//已同意（发起方）
        private const val ITEM_ALREADY_REFUSE_FAQIFANG_TYPE = 6//已拒绝（发起方）

    }


    interface OnItemClickListener {
        fun onClick(type: Int, position: Int)
    }
}